package com.dongchen.designpatter.chapter05.methodfactory.sample01;

import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

/**
 * @author dongchen
 * @time 2021/9/17
 */
@Slf4j
public class XMLUtil {
    public static Object getBean() {

        try {
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("D:\\Java_Code\\java-design-pattern\\src\\main\\java\\com\\dongchen\\designpatter\\chapter05\\methodfactory\\sample01\\configTV.xml"));
            NodeList nodeList = doc.getElementsByTagName("className");
            Node node = nodeList.item(0).getFirstChild();
            String brandName = node.getNodeValue().trim();
            Class clazz = Class.forName(brandName);
            return clazz.newInstance();
        } catch (Exception ex) {
            log.error("Parse config.xml file error  , errorMsg = {}", ex.getMessage(), ex);
        }
        return null;


    }
}
